package ar.edu.unlp.info.tesis.database.operator;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.sql.JoinFragment;

public class AliasCriteria {
	/**
	 * clase que representa una union entre dos objetos (representados por
	 * tablas) el tipo de union esta dado por el tercer parametro, por defecto
	 * se utiliza INNER JOIN, otras opciones posibles: FULL_JOIN,
	 * LEFT_OUTER_JOIN, RIGHT_OUTER_JOIN
	 * 
	 */
	private String atributo = "";

	private String alias = "";

	private String joinType = "INNER_JOIN";

	public AliasCriteria(String alias, String atributo, String type) {
		super();
		this.alias = alias;
		this.atributo = atributo;
		joinType = type;
	}

	public void setCriteria(Criteria criteria) {
		criteria.createAlias(atributo, alias, getJoinOption(joinType));
	}

	private int getJoinOption(String joinType) {
		if (joinType.equals("INNER_JOIN"))
			return JoinFragment.INNER_JOIN;
		if (joinType.equals("FULL_JOIN"))
			return JoinFragment.FULL_JOIN;
		if (joinType.equals("LEFT_OUTER_JOIN"))
			return JoinFragment.LEFT_OUTER_JOIN;
		if (joinType.equals("RIGHT_OUTER_JOIN"))
			return JoinFragment.RIGHT_OUTER_JOIN;
		return 0;
	}

	public AliasCriteria(String atributo, String alias) {
		this.alias = alias;
		this.atributo = atributo;
	}

	public static void addAlias(List<AliasCriteria> listAliasCriteria,
			String atributo, String alias, String joinType) {
		if (joinType == null)
			joinType = "INNER_JOIN";

		// verifico que ya no exista un alias con este nombre
		for (AliasCriteria aliasCriteria : listAliasCriteria) {
			if (alias.equals(aliasCriteria.getAlias()))
				return;
		}
		listAliasCriteria.add(new AliasCriteria(alias, atributo, joinType));
	}

	public String getAlias() {
		return alias;
	}

	public void setAlias(String alias) {
		this.alias = alias;
	}

	public String getAtributo() {
		return atributo;
	}

	public void setAtributo(String atributo) {
		this.atributo = atributo;
	}

	public String getJoinType() {
		return joinType;
	}

	public void setJoinType(String joinType) {
		this.joinType = joinType;
	}

}
